<template>
    <div class="popProject-select">
        <el-dialog
            :title="is_bill ? '开单' : '选择服务'"
            :visible.sync="dialogVisible"
            top="5vh"
        >
            <div class="search-box" v-show="!is_bill">
                <div class="search-box-select">
                    <el-form :model="searchform">
                        <el-form-item>
                            <el-cascader
                                v-model="searchform.catid"
                                :options="cat_ls"
                                class="sele-w"
                                :props="propsdata"
                                @change="changeList"
                            ></el-cascader>
                        </el-form-item>
                        <el-form-item>
                            <el-select
                                v-model="porstatus"
                                class="sele-w"
                                @change="changePorstatus"
                            >
                                <el-option
                                    v-for="item in ucp_ls_data"
                                    :key="item.uid"
                                    :label="item.name"
                                    :value="item.uid"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-form>
                </div>
                <div class="list-box" v-infinite-scroll="loadFun">
                    <div class="list" v-if="list_data.length > 0">
                        <div
                            class="list-row"
                            v-for="item in list_data"
                            :key="item.id"
                            @click="cheproject(item)"
                        >
                            <div class="list-info">
                                <div class="img-box">
                                    <img
                                        :src="item.imgs ? item.imgs : img_buf"
                                        alt=""
                                    />
                                </div>
                                <div class="info-box">
                                    <div class="title">
                                        <span>{{ item.title }}</span>
                                    </div>
                                    <div class="phone">
                                        <span>{{ item.txt_title }}</span>
                                    </div>
                                    <div class="tiem">
                                        <span>{{ item.price }}</span>
                                        <span
                                            class="f-num"
                                            v-if="item.fillingData"
                                            >{{ item.fillingData.number }}</span
                                        >
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <empty v-else />
                </div>
                <span slot="footer" class="dialog-footer">
                    <div class="num">
                        <span>数量：{{ totalNum || 0 }}</span
                        ><span>总额：{{ totalpic || 0 }}</span>
                    </div>
                    <div class="but">
                        <el-button @click="cancelFun">取 消</el-button>
                        <el-button
                            type="primary"
                            @click="determineFun"
                            v-show="!is_bill"
                            >选 好 了</el-button
                        >
                    </div>
                </span>
            </div>
            <popProjectFilling
                ref="popProjectFilling"
                v-show="is_bill"
                @cancelFun="cancelFun"
                @fillingData="fillingData"
                @editData="editData"
            ></popProjectFilling>
        </el-dialog>
    </div>
</template>

<script>
import popProjectFilling from "./popProjectFilling.vue";
import staticConst from "../../js/const/StaticConst.js";
import config from "@/js/conf/Config.js";
import empty from "../comm/empty.vue";

export default {
    name: "popProjectSelect",
    props: ["customerid"],
    components: { popProjectFilling, empty },
    data() {
        return {
            img_buf: staticConst.GOOD_IMG,
            dialogVisible: false,
            is_soll: false,
            is_bill: false,
            list_data: [],
            selectedData: [],
            selectedInfo: null,
            totalNum: 0,
            totalpic: 0,
            porstatus: "",
            searchform: {
                catid: "",
                page: "1",
                pnum: 20,
                pfrom: -20,
            },
            cat_ls: [], //分类列表

            ucp_ls_data: [
                {
                    name: "全部服务",
                    uid: "",
                },
                {
                    name: "已选服务",
                    uid: "1",
                },
            ],

            propsdata: {
                children: "son",
                label: "title",
                value: "id",
                expandTrigger: "hover",
                checkStrictly: true,
            },
        };
    },
    methods: {
        initData(data) {
            this.dialogVisible = true;
            this.is_bill = false;
            this.searchform.catid = "";
            this.porstatus = "";
            this.cat_ls = [];
            this.changeList();
        },
        projectDetails(data) {
            this.dialogVisible = true;
            this.is_bill = true;
            this.is_soll = true;
            let type = data.fillingForm ? "1" : "2";
            this.$nextTick(() => {
                this.$refs.popProjectFilling.initData(data, type, true);
            });
        },
        changePorstatus(val) {
            if (val == 1) {
                this.list_data = [];
                this.is_soll = true;
            } else {
                this.changeList();
            }
        },
        fillingData(data) {
            this.totalNum = 0;
            this.totalpic = 0;
            this.list_data.map((v) => {
                if (v.id == data.parameter.id) {
                    v.fillingData = data.fillingData;
                    v.fillingForm = data.fillingForm;
                }
                if (v.fillingForm) {
                    this.totalNum += Number(v.fillingForm.number);
                    this.totalpic +=
                        Number(v.fillingForm.number) *
                        Number(v.fillingForm.price) *
                        (Number(v.fillingForm.discount_r) * 0.01);
                }
            });
            this.totalpic = this.totalpic.toFixed(2);
            this.is_bill = false;
        },
        editData(data) {
            this.dialogVisible = false;
            this.$emit("editData", data);
        },
        cancelFun() {
            if (this.is_bill) {
                this.is_bill = false;
            } else {
                this.dialogVisible = false;
            }
        },
        // 确定服务选择
        determineFun() {
            const data = {
                allCount: 0,
                customerid: this.customerid,
                spec_unit_ls_m: [],
                totalMoney: 0,
                type: "1",
            };
            this.list_data.map((v) => {
                if (v.fillingData) {
                    data.spec_unit_ls_m.push(v.fillingData);
                }
            });

            if (data.spec_unit_ls_m.length == 0) {
                this.$msg({ title: "请选择产品" });
                return;
            }
            this.$emit("getProjectData", data);
        },

        // 选中服务
        cheproject(item) {
            let type = item.fillingForm ? "1" : "2";
            const data = {
                porjectData: item,
                customerid: this.customerid,
                fillingForm: item.fillingForm || {},
            };
            this.$refs.popProjectFilling.initData(data, type);
            this.is_bill = true;
        },

        // 滚动加载
        loadFun() {
            if (this.is_soll) return;
            this.searchform.pfrom += 20;
            this.getlistdata();
        },

        // 筛选服务
        changeList() {
            this.emptyData();
            this.getlistdata();
        },

        // 获取服务列表
        async getlistdata() {
            this.is_soll = true;
            let url = config.YY_GOODSINFO_GOODS_LIST;
            let param = Object.assign({}, this.searchform);
            param.catid = param.catid
                ? param.catid[param.catid.length - 1]
                : "";
            let res = await this.$api({ url: url, data: param });
            if (res.status == 1) {
                if (
                    res.data.cat_ls &&
                    res.data.cat_ls &&
                    this.cat_ls.length == 0
                ) {
                    this.cat_ls = res.data.cat_ls;
                    this.searchform.catid = "all";
                }
                if (res.data.goods_ls) {
                    this.list_data = this.list_data.concat(res.data.goods_ls);
                    setTimeout(() => {
                        this.is_soll = false;
                    }, 200);
                } else {
                    this.is_soll = true;
                }
            }
        },

        // 清空数据
        emptyData() {
            this.searchform.pfrom = 0;
            this.list_data = [];
            this.selectedData = [];
            this.is_soll = false;
            this.totalNum = 0;
            this.totalpic = 0;
        },
    },
};
</script>

<style lang="scss" scoped>
.popProject-select {
    ::v-deep.search-box-select {
        .el-form {
            display: flex;
        }
        .sele-w {
            margin-right: 10px;
        }
    }
}
.dialog-footer {
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .num {
        font-size: 14px;
        span {
            margin-right: 10px;
        }
    }
}
.list-box {
    height: 520px;
    overflow: auto;
    .list-row {
        padding: 10px 0;
        border-bottom: 1px #f2f2f2 solid;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-right: 30px;
        cursor: pointer;
        .list-info {
            display: flex;
            width: 100%;
            .img-box {
                width: 80px;
                height: 80px;
                margin-right: 10px;
                border-radius: 5px;
                background: #eaeaea;
                overflow: hidden;
                display: flex;
                justify-content: center;
                align-items: center;
                img {
                    width: 100%;
                    height: 100%;
                    object-fit: cover;
                }
                span {
                    font-size: 16px;
                    font-weight: bold;
                }
            }
            .info-box {
                width: calc(100% - 80px);
                .title {
                    font-size: 14px;
                }
                .phone {
                    font-size: 12px;
                    color: #9c9a9a;
                    margin: 5px 0;
                    span {
                        margin-right: 10px;
                    }
                }
                .tiem {
                    width: 100%;
                    font-size: 12px;
                    color: #fa4848;
                    display: flex;
                    justify-content: space-between;
                    .f-num {
                        padding: 0 20px;
                        background: #3388ff;
                        color: #fff;
                        font-size: 12px;
                        margin-left: 10px;
                    }
                }
            }
        }
        .list-avitve {
            font-size: 20px;
            color: #3388ff;
        }
    }
}
.list-box::-webkit-scrollbar {
    /*滚动条整体样式*/
    width: 5px; /*高宽分别对应横竖滚动条的尺寸*/
    height: 1px;
}
.list-box::-webkit-scrollbar-thumb {
    /*滚动条里面小方块*/
    border-radius: 10px;
    -webkit-box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.1);
    background: #ccc; //rgba(0, 0, 0, 0.0);
    scrollbar-arrow-color: red;
}
.list-box::-webkit-scrollbar-track {
    /*滚动条里面轨道*/
    -webkit-box-shadow: inset 0 0 10px rgba(0, 0, 0, 0);
    border-radius: 0;
    background: rgba(0, 0, 0, 0);
}
</style>